//
//  XMHomeMeetCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/10.
//

#import "XMHomeMeetCell.h"

@interface XMHomeMeetCell ()

@property (nonatomic, strong) UIImageView *headImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *creditLabel;

@end

@implementation XMHomeMeetCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIView *contentView = [[UIView alloc] initWithFrame:self.bounds];
    contentView.backgroundColor = kColorHEX(0xffffff);
    [contentView setCornerRadius:KScaleWidth(13.0)];
    [self addSubview:contentView];
    [contentView addSubview:self.headImgView];
    [contentView addSubview:self.titleLabel];
    [contentView addSubview:self.priceLabel];
    [contentView addSubview:self.dateLabel];
    [contentView addSubview:self.avatarImgView];
    [contentView addSubview:self.nameLabel];
    [contentView addSubview:self.creditLabel];
}

- (void)setContentWithMeetModel:(ZMYCommend *)meetModel {
    [_headImgView sd_setImageWithURL:[NSURL URLWithString:meetModel.coverImageUrl]];
    CGFloat height = [meetModel.title getHeightWithFont:KScaleWidth(16.0) withWidth:_titleLabel.width] + KScaleWidth(10.0);
    _titleLabel.height = MAX(height, KScaleWidth(32.0));
    _titleLabel.text = meetModel.title;
    _priceLabel.y = _titleLabel.maxY;
    NSMutableAttributedString *priceAttr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@", meetModel.price] attributes:@{NSFontAttributeName : KBoldFontSize(18.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39)}];
    [priceAttr addAttributes:@{NSFontAttributeName : KFontSize(10.0)} range:NSMakeRange(0, 1)];
    [_priceLabel setAttributedText:priceAttr];
    _dateLabel.y = _priceLabel.y;
    _dateLabel.text = [self getDateStrWithInterval:[meetModel.createTime integerValue]/1000];
    _avatarImgView.y = _priceLabel.maxY + KScaleWidth(6.0);
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:meetModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.y = _avatarImgView.y;
    _nameLabel.text = meetModel.nickname;
    _creditLabel.y = self.nameLabel.y + (_nameLabel.height - _creditLabel.height)/2;
}

- (NSString *)getDateStrWithInterval:(NSInteger)interval {
    NSInteger currentTimeInterval = [[NSDate date] timeIntervalSince1970];
    NSInteger distance = currentTimeInterval - interval;
    NSString *currentDate = [[NSDate date] dateStringWithFormat:@"yyyyMMdd"];
    NSString *pubDate = [[NSDate dateWithTimeIntervalSince1970:interval] dateStringWithFormat:@"yyyyMMdd"];
//    NSString *current
    if ([currentDate integerValue] - [pubDate integerValue] > 10000) {
        return [NSString stringWithFormat:@"发布于去年%@", [[NSDate dateWithTimeIntervalSince1970:interval] dateStringWithFormat:@"MM/dd"]];
    }
    else if ([currentDate integerValue] - [pubDate integerValue] > 1) {
        return [NSString stringWithFormat:@"发布于%@", [[NSDate dateWithTimeIntervalSince1970:interval] dateStringWithFormat:@"MM/dd HH:ss"]];
    }
    else if ([currentDate integerValue] - [pubDate integerValue] == 1) {
        return @"发布于昨天";
    }
    else {
        return [NSString stringWithFormat:@"发布于%.2f小时前", distance/60.0/60.0];
    }
}

#pragma mark - Init
- (UIImageView *)headImgView {
    if (!_headImgView) {
        _headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(210.0))];
        
    }
    return _headImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.headImgView.maxY + KScaleWidth(5.0), self.width - KScaleWidth(10.0)*2, KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.titleLabel.maxY, self.width/2 - KScaleWidth(10.0), KScaleWidth(25.0))];
    }
    return _priceLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/3, self.priceLabel.y, self.width/3*2 - KScaleWidth(10.0), _priceLabel.height) font:KFontSize(12.0) textColor:kColorHEX(0x999999) textAlignment:NSTextAlignmentRight];
    }
    return _dateLabel;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.priceLabel.maxY + KScaleWidth(6.0), KScaleWidth(20.0), KScaleWidth(20.0))];
        _avatarImgView.contentMode = UIViewContentModeScaleAspectFit;
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(3.0), _avatarImgView.y, self.width - _avatarImgView.maxX - KScaleWidth(53.0), _avatarImgView.height) font:KFontSize(12.0) textColor:kColorHEX(0x999999)];
    }
    return _nameLabel;
}

- (UILabel *)creditLabel {
    if (!_creditLabel) {
        _creditLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - KScaleWidth(10.0) - KScaleWidth(43.0), self.nameLabel.y + (_nameLabel.height - KScaleWidth(14.0))/2, KScaleWidth(43.0), KScaleWidth(14.0)) font:KFontSize(10.0) textColor:kColorHEX(0xF27A3D) textAlignment:NSTextAlignmentCenter];
        _creditLabel.backgroundColor = kColorHEX(0xFFF5EC);
        [_creditLabel setCornerRadius:_creditLabel.height/2];
        _creditLabel.text = @"信用良好";
        
    }
    return _creditLabel;
}

@end
